/**
 ******************************************************************************* *
 * @author      Ashwin Vijayakumar
 *
 * @file        main.c
 * 
 * @date        Sep 8, 2012 9:05:24 AM
 *******************************************************************************
 * @copyright 2012
 * <a href="http://www.afflatustech.com">Afflatus Techtronics</a>
 * and <a href="http://www.ashwinvijayakumar.co.nr">Ashwin Vijayakumar</a>
 *******************************************************************************
 */

/**@cond -----------------------------------------------------------------------
 * Includes section
 * ----------------------------------------------------------------- @endcond */

/**@cond System headers -------------------------------------------- @endcond */
#include <htc.h>

/**@cond Local headers --------------------------------------------- @endcond */
#include "MorseCode.h"

/* Device Configurations */
__CONFIG( FOSC_INTRCCLK &       /* Internal oscillator, I/O function on GP4/5 */
    WDTE_OFF &                  /* Watchdog timer off */
    PWRTE_OFF &                 /* PowerUp timer disabled */
    MCLRE_OFF &                 /* MCLR internally tied to VDD, I/O on GP3 */
    BOREN_OFF &                 /* Brown-Out Detect disabled */
    CP_OFF &                    /* Program memory protection disabled */
    CPD_OFF );                  /* Data memory protection disabled */

/**@cond -----------------------------------------------------------------------
 * typedef section (File scope)
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Constants section (File scope)
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Macros section (File scope)
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Global variables section (File scope)
 * ----------------------------------------------------------------- @endcond */

/* Duty cycle in percentage (0 to 100) */
static unsigned short int duty_cycle = 0;

static char morse_heart_beat[11] = {"0123456124\0"};

/**@cond -----------------------------------------------------------------------
 * Function prototypes section (File scope)
 * ----------------------------------------------------------------- @endcond */

static void GpioInitialize( void );

static void TimerInitialize( void );

static void HeartBeat( void );

/**@cond -----------------------------------------------------------------------
 * Getter/setter method section (Project scope)
 * ----------------------------------------------------------------- @endcond */

void
main( void )
{
  int i, j;
#if 0
  /* Set internal oscillator to max frequency (4MHz) */
  OSCCAL = 0xFC;
#endif
  /* GPIO initializations */
  GpioInitialize();

  /* Timer 0 initializations for PWM (LED brightness control) */
  TimerInitialize();

  while( 1 )
    {
      for( i = 0; i < 8; i++ )
        HeartBeat();

      for( i = 0; i < 30000; i++ );

      MorseCode_Puts( morse_heart_beat );

      for( i = 0; i < 30000; i++ );

      MORSE_CODE_PIN = 0;
      for( j = 0; j < 6; j++ )
        for( i = 0; i < 30000; i++ );
      MORSE_CODE_PIN = 1;
    }
}

static void interrupt
isr(void)
{
  /* Disable Global interrupt to avoid nested interrupts */
  INTCONbits.GIE = 0;

  /* Check if Timer 0 caused the interrupt */
  if( INTCONbits.T0IF )
    {
      static int pulse_high = 0;
      static int t0_counter = 1;

      if( t0_counter++ >= 100 )
        t0_counter = 1;

      if( t0_counter <= duty_cycle )
        MORSE_CODE_PIN = 1;
      else
        MORSE_CODE_PIN = 0;

      /* Reload Timer 0 register */
      TMR0 = 240;

      /* Reset Timer 0 Interrupt Flags */
      INTCONbits.T0IF = 0;
    }

  /* Global interrupt enable */
  INTCONbits.GIE = 1;
}

static void
GpioInitialize( void )
{
  /* Set GP5 direction to output */
  TRISIObits.TRISIO5 = 0;

  /* Enable internal weak pull-up */
  WPUbits.WPU5 = 1;

  /* Initially LED is OFF */
  MORSE_CODE_PIN = 1;
}

static void
TimerInitialize( void )
{
  /* Timer 0 increments on instruction clock */
  T0CS = 0;

  /* Assign prescaler to Timer 0 */
  OPTION_REGbits.PSA = 0;

  /* Prescaler 1:256 */
  OPTION_REGbits.PS = 1;

  /* Enable interrupt on TMR0 overflow */
  INTCONbits.T0IE = 1;
}

static void
HeartBeat( void )
{
  int i;

  /* Global interrupt enable */
  INTCONbits.GIE = 1;

  duty_cycle = 0;
  while(1)
    {
      if( duty_cycle++ >= 100 )
        break;
      for( i = 0; i < 60; i++ );
    }

  duty_cycle = 100;
  for( i = 0; i < 1500; i++ );

  duty_cycle = 0;
  while(1)
    {
      if( duty_cycle++ >= 100 )
        break;
      for( i = 0; i < 60; i++ );
    }

  duty_cycle = 100;
  for( i = 0; i < 10000; i++ );


  /* Global interrupt disable */
  INTCONbits.GIE = 0;
}

/** END OF FILE ********************* Copyright (C) 2012 Afflatus Techtronics */
